/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import java.util.LinkedHashMap;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.Industry;
import java.util.List;
import javax.persistence.Query;
import local.IndustryFacadeLocal;

/**
 *
 * @author Ken
 */
@Stateless
public class IndustryFacade implements IndustryFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public List<Industry> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Industry.class));
        return em.createQuery(cq).getResultList();
    }

    @Override
    public Industry find(long industry_id) {
        return em.find(Industry.class, industry_id);
    }
    
    @Override
    public LinkedHashMap<String, String> getAllIndustry() {
        Query q = em.createQuery("SELECT i FROM Industry i");
        LinkedHashMap<String, String> sortedMap = new LinkedHashMap<String, String>();

        for (Object o : q.getResultList()) {
            Industry ind = (Industry) o;
            Long indID = ind.getId();
            sortedMap.put(indID.toString(), ind.getName());

        }
        return sortedMap;
    }

    @Override
    public Industry[] getAllIndustryList() {
        Query q = em.createQuery("SELECT i FROM Industry i");

        int resultListSize = q.getResultList().size();
        Industry[] indArr = new Industry[resultListSize];

        for (int i = 0; i < resultListSize; i++) {
            Object o = q.getResultList().get(i);
            Industry ind = (Industry) o;
            indArr[i] = ind;
        }
        return indArr;
    }

    @Override
    public String getIndustryID(String industryName) {
        Query q = em.createQuery("SELECT i FROM Industry i WHERE i.name = ?1");
        q.setParameter(1, industryName);

        for (Object o : q.getResultList()) {
            Industry ind = (Industry) o;
            Long indID = ind.getId();
            return indID.toString();

        }
        return "";
    }
}
